import logging

from eshop.lib.base import *
import sha

#from model import *

log = logging.getLogger(__name__)

class AccountController(BaseController):

    def login(self):
        return render('/account/login.mako')
        
    def logout(self):
        if request.environ.has_key('REMOTE_USER'):
            del request.environ['REMOTE_USER']
        return render('/account/logout.mako')        
    
    def signin_check(self):
        username = request.params.get('username', '').strip().lower()
        password = request.params.get('password', '').strip()        
        user = model.User.q.filter_by(username=username).first()            
        if not user or user.password != password :
            log.info('Fail to login user=%(Name)s from ip=%(IP)s' % {'Name' : username, 'IP' : request.environ.get('REMOTE_ADDR')})
            c.message="No such user %s" % username
            return render('/account/login.mako')
        request.environ['paste.auth_tkt.set_user'](username)
        return redirect_to(request.environ['HTTP_REFERER'])